<?php
/**
 * Created by PhpStorm.
 * User: Jason
 * Date: 2017/9/20
 * Time: 1:01
 */

namespace Home\Controller;


use Home\Service\PageService;
use Home\Service\ScoreService;
use Home\Service\SearchKeywordsService;
use Home\Service\SheetService;
use Think\Page;

class SearchController extends HomeController
{
    /**
     * 默认曲谱搜索
     */
    public function index()
    {
        $keywords = explode(' ', urldecode(I('name')));
        $keywordArr = array();
        //记录关键字
        $service = new SearchKeywordsService();
        $service->record($keywords);
        if (!empty($keywords)) {
            foreach ($keywords as $key => $keyword) {
                $keywordArr[] = array('like', '%'.$keyword.'%');
            }
        }
        $map = array(
            'name' => $keywordArr,
            'is_delete' => 0
        );

        //1.获取曲谱搜索结果
        $count = M('Sheet')->where($map)->count();

        $page = new PageService($count, 10);
        $show = $page->show();
        $sheetList = M('Sheet')->where($map)->order('id')->limit($page->firstRow . ',' . $page->listRows)->select();
        if (!empty($sheetList)) {
            $sheetService = new SheetService();
            $sheetList = $sheetService->getSheetInfo($sheetList);
        }
        $this->assign('sheetList', $sheetList);
        $this->assign('page', $show);
        $this->assign('sheetResCount', $count);


        //2.获取谱单搜索结果
        $scoreCount = M('Score')->where($map)->count();

        $scorePage = new PageService($scoreCount, 10);
        $scoreList = M('Score')->where($map)->order('id')->limit($scorePage->firstRow . ',' . $scorePage->listRows)->select();
        if (!empty($scoreList)) {
            $scoreService = new ScoreService();
            $scoreList = $scoreService->scoreInfo($scoreList);
        }
        $scoreShow = $scorePage->show();
        $this->assign('scoreList', $scoreList);
        $this->assign('scorePage', $scoreShow);
        $this->assign('scoreResCount', $scoreCount);

        //3.获取用户搜索结果
        $map = array(
            'nickname' => $keywordArr,
            'is_delete' => 0,
            'is_alias' => 0
        );
        $userCount = M('User')->where($map)->count();
        $userPage = new PageService($userCount, 10);
        $scoreShow = $userPage->show();
        $userList = M('User')->where($map)->order('id')->limit($userPage->firstRow . ',' . $userPage->listRows)->select();
        if (!empty($userList)) {
            //获取用户曲谱数和播放次数
        }
        $this->assign('userList', $userList);
        $this->assign('userPage', $userPage);
        $this->assign('userResCount', $userCount);

        $this->assign('keywords', urldecode(I('name')));
        $this->display();
    }

    /**
     * 热搜榜
     */
    public function rank()
    {
        $param = I('param.');
        $page = is_numeric($param['p']) ? $param['p'] : 1;
        //获取数据表搜索记录
        $service = new SearchKeywordsService();
        $map = $service->keywordsMap();
        if (!$map) {
            unset($map);
        }
        //获取所有曲谱
        $service = new SheetService();
        $map['is_delete'] = 0;
        $order = 'click_count desc';
        $list = $service->topRank($map, $page, 25, $order, $count);
        $this->assign('sheetList', $list);
        //分页
        $Page       = new \Think\Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数
        $show       = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出

        $this->assign('nav', 'search');
        $this->assign('update_time', date('Y.m.d', time()));
        $this->display('Search/rank');
    }
}